package role

import (
	"ginfwk/adapter/base/controller"
	dto "ginfwk/application/dto/admin"
	"ginfwk/common/mock/admin"
	"ginfwk/common/mock/mhelper"
	"ginfwk/infrastructure/helper/ghttp"
	"github.com/gin-gonic/gin"
)

type Role struct {
	controller.BaseController
}

func (c *Role) Init(g *gin.Context) {
	c.BaseController.Init(g)
}

func (c *Role) Options(g *gin.Context) interface{} {
	roleList := admin.GetRoleList()
	return ghttp.Success(gin.H{
		"list": roleList,
	})
}

func (c *Role) List(g *gin.Context) interface{} {
	type ListInfo struct {
		PageIndex int    `json:"pageIndex" form:"pageIndex"`
		PageSize  int    `json:"pageSize" form:"pageSize"`
		RoleName  string `json:"roleName" form:"roleName"`
	}
	var info ListInfo
	if err := g.ShouldBindQuery(&info); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	roleList := admin.GetRoleList()
	pageList := mhelper.Pagination(roleList, info.PageIndex, info.PageSize)

	return ghttp.Success(gin.H{
		"list":  pageList,
		"total": len(roleList),
		"info":  info,
	})
}

func (c *Role) Info(g *gin.Context) interface{} {
	type Param struct {
		Id string `json:"id" form:"id"`
	}
	var param Param
	if err := g.ShouldBindQuery(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	var roleInfo dto.RoleInfo
	roleList := admin.GetRoleList()
	for _, role := range roleList {
		if role.Id == param.Id {
			roleInfo = role
		}
	}
	return ghttp.Success(roleInfo)
}

func (c *Role) Save(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		Id          string `json:"id" form:"id"`
		RoleName    string `json:"roleName" form:"roleName"`
		Role        string `json:"role" form:"role"`
		CategoryTag string `json:"categoryTag" form:"categoryTag"`
		Status      int    `json:"status" form:"status"`
	}
	var param Param
	if err := g.ShouldBindJSON(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	param.Id = "xxxx3"
	return ghttp.Success(param)
}

func (c *Role) Delete(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		Ids []string `json:"ids" form:"ids"`
	}
	var param Param
	if err := g.ShouldBindJSON(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	return ghttp.Success(param)
}
